layui.use(['layer', 'form', 'table'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;

    var realAccountId="";
    var realGroupId="";
    var realMsisdn="";
    var realStatus="";

    /**
     * 套餐资费--可订购资费目录节点
     */
    var OrderedCharges = {
        tableId: "orderedChargesTable",    //表格id
        condition: {
            status: "",
            accountId:"",
            catalogId:"",
            categoryId:"",
        }
    };

    /**
     * 查询所有的账号信息，填充select标签
     */
    function searchAccount(accountId) {
        $('#accountId').html('');
        $("#accountId").append('<option value="" >请选择</option>');
        $.ajax({
            type: 'get',
            url: Feng.ctxPath + "/account/allSelectAccounts",
            async: false,
            success: function (data) {
                for (var i = 0; i < data.data.length; i++) {
                    var name = data.data[i].accountName;
                    var code = data.data[i].accountId;
                    if (accountId == code) {
                        $("#accountId").append('<option value="' + code + '" selected>' + name + '</option>');
                    } else {
                        $("#accountId").append('<option value="' + code + '">' + name + '</option>');
                    }
                }
                form.render('select');
            }
        });
    }
    searchAccount();

    /**
     * 初始化表格的列
     */
    OrderedCharges.initColumn = function () {
        return [[
            {field: 'catalogId', align: "center", title: '目录ID'},
            {field: 'categoryId', align: "center", title: '节点ID'},
            {field: 'categoryName', align: "center", title: '节点名称'},
            {field: 'categoryDesc', align: "center", title: '节点描述'},
            {field: 'categoryOrder', align: "center", title: '节点操作员显示顺序'},
            {field: "categoryParent", align: "center", title: '父节点ID'},
            {align: "center", toolbar: '#charges', title: '查看资费', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderedCharges.searchOrderedCharges = function () {
        var accountId = $("#accountId").val();
        realAccountId = $("#accountId").val();
        realGroupId = $("#groupId").val();
        realMsisdn = $("#msisdn").val();
        realStatus = $("#status").val();
        var queryData = {};
        queryData['accountId'] = $("#accountId").val();
        queryData['status'] = $("#status").val();
        queryData['msisdn'] = $("#msisdn").val();
        queryData['groupId'] = $("#groupId").val();
        table.reload(OrderedCharges.tableId, {
            where: queryData
        });
        searchAccount(accountId);//每次点查询就重置;
    };
    $("#btnSearch").click(function () {
        OrderedCharges.searchOrderedCharges();
    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderedCharges.tableId,
        url: Feng.ctxPath + '/orderedCharges/list',
        page: false,
        height: "full-98",
        cellMinWidth: 100,
        cols: OrderedCharges.initColumn()
    });

    //工具条点击事件
    table.on('tool(' + OrderedCharges.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'searchCharges' && realStatus === '1') {
            OrderedCharges.searchEcCharges(data);
        }
        if(layEvent === 'searchCharges' && realStatus === '2') {
            OrderedCharges.searchGroupCharges(data);
        }
        if(layEvent === 'searchCharges' && realStatus === '3') {
            OrderedCharges.searchUserCharges(data);
        }
    });

    /**
     * 查看集团可订购资费
     *
     * @param data 点击按钮时候的行数据
     */
    OrderedCharges.searchEcCharges = function (data) {
        layer.open({
            type: 2,
            title: '可订购资费',
            area: ['1000px', '550px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/orderedCharges/toEcOrderedCharges/' + realAccountId + '/' + realStatus + '/' + data.catalogId + '/' + data.categoryId
        });
    };

    /**
     * 查看群组可订购资费
     *
     * @param data 点击按钮时候的行数据
     */
    OrderedCharges.searchGroupCharges = function (data) {
        if (realGroupId == "") {
            Feng.error("群组Id不能为空！");
            return;
        }
        layer.open({
            type: 2,
            title: '可订购资费',
            area: ['1000px', '550px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/orderedCharges/toGroupOrderedCharges/' + realAccountId + '/' + realStatus + '/' + data.catalogId + '/' + data.categoryId + '/' + realGroupId
        });
    };

    /**
     * 查看用户可订购资费
     *
     * @param data 点击按钮时候的行数据
     */
    OrderedCharges.searchUserCharges = function (data) {
        if (realMsisdn == "") {
            Feng.error("Msisdn不能为空！");
            return;
        }
        layer.open({
            type: 2,
            title: '可订购资费',
            area: ['1000px', '550px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/orderedCharges/toUserOrderedCharges/' + realStatus + '/' + data.catalogId + '/' + data.categoryId + '/' + realMsisdn
        });
    };

    //控制查询条件随着select选中项变化而变化
    $("#showAccount").show();
    $("#showGroup").hide();
    $("#showMsisdn").hide();
    form.on('select(status)', function(data){
        var value = data.value;
        if(value == "1"){
            $("#showAccount").show();
            $("#showGroup").hide();
            $("#showMsisdn").hide();
        }else if(value == "2"){
            $("#showAccount").show();
            $("#showGroup").show();
            $("#showMsisdn").hide();
        }else{
            $("#showAccount").hide();
            $("#showGroup").hide();
            $("#showMsisdn").show();
        }
    })
});